//
//  LBMediaPresentViewController.m
//  LangBa
//
//  Created by Mac on 2017/12/21.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "LGCultureCommentPresentController.h"

@interface LGCultureCommentPresentController ()<UITextFieldDelegate>
@property(nonatomic, strong) STButton                     *clearButton;
@property(nonatomic, strong) UIView                     *customView;
@property(nonatomic, strong) UITextField                     *textFiled;
@property(nonatomic, strong) NSMutableArray                     *buttonsArray;
@property(nonatomic, strong) UIView                     *bootomeView;
@end

@implementation LGCultureCommentPresentController

- (instancetype)init
{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    return self;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self configSubView];
    [self st_addNotifacations];

}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.textFiled becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --Notifacation
- (void)st_addNotifacations{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(st_keyboardWillChangeFrameNotification:)
                                                 name:UIKeyboardWillChangeFrameNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(st_keyboardWillHideNotification:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
}
- (void)st_keyboardWillChangeFrameNotification:(NSNotification*)notify{
    
    // 动画的持续时间
    NSDictionary *userInfo = notify.userInfo;
    double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    UIView * view = [[UIView alloc] initWithFrame:keyboardF];
    
    self.bootomeView.bottom = view.top;
    
}
- (void)st_keyboardWillHideNotification:(NSNotification*)notify{
    // 动画的持续时间
    NSDictionary *userInfo = notify.userInfo;
    double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    // 键盘的frame
    UIView * view = [[UIView alloc] initWithFrame:keyboardF];

    [UIView animateWithDuration:duration animations:^{
        self.bootomeView.top = [TMUtils tabBarTop];
    } completion:^(BOOL finished) {
        
    }];
    
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.clearButton = [[STButton alloc] initWithFrame:UIScreenFrame
                                                 title:nil
                                            titleColor:nil
                                             titleFont:0
                                          cornerRadius:0
                                       backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                       backgroundImage:nil
                                                 image:nil];
    [weakSelf.clearButton setClicAction:^(UIButton *sender) {
        [weakSelf dismiss];
    }];
    [self.view addSubview:_clearButton];
    

    self.bootomeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 49)];
    self.bootomeView.backgroundColor = [UIColor whiteColor];
    [self.bootomeView st_showTopShadow];
    self.bootomeView.bottom = [TMUtils tabBarTop]  +49;
    [self.clearButton addSubview:self.bootomeView];
    
    self.textFiled = [[UITextField alloc] initWithFrame:CGRectMake(15, 0, UIScreenWidth - 15 , 49)];
    self.textFiled.placeholder = @"请输入内容";
    self.textFiled.returnKeyType = UIReturnKeySend;
    self.textFiled.delegate = self;
    [self.bootomeView addSubview:self.textFiled];
    
    STButton * sendButton = [[STButton alloc] initWithFrame:CGRectMake(0, 2.5, 44, 44)
                                                     title:@""
                                                titleColor:FlatWhite
                                                 titleFont:15
                                              cornerRadius:5
                                           backgroundColor:[UIColor clearColor]
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"44"]];
    sendButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    sendButton.right = UIScreenWidth -15;
    [sendButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedChoseIamgeButton];
    }];
  //  [self.bootomeView addSubview:sendButton];
    
    //sub
    
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@"\n"]) {
        [self onSelectedSendButton];
    }
    return YES;
}
#pragma mark --Private Method
- (void)show{
    [UIView animateWithDuration:0.6
                          delay:0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.7
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
                         self.customView.top = 100;
                     } completion:^(BOOL finished) {
                         
                     }];
}
- (void)dismiss{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}
- (void)dismissWithHanle:(void(^)())handle{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (handle) {
                handle();
            }
        }];
    }];
}
#pragma mark --Action Method
- (void)onSelectedCommitButton{
    
    
}
- (void)onSelectedCancleButton{
    
    
}
- (void)onSelectedWhiteView{
    [self.view endEditing:YES];
}
- (void)onSelctedChoseIamgeButton{
    [self dismissWithHanle:^{
        if (self.onSelctedSendImageDismissHandle) {
            self.onSelctedSendImageDismissHandle();
        }
    }];

}
- (void)onSelectedSendButton{
    
    if (!self.textFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"您还没有输入任何评论"];
        return;
    }
    [self dismiss];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.onSelctedSendHandle) {
            self.onSelctedSendHandle(self.textFiled.text);
        }
    });

}
- (void)onSelectedTagButton:(UIButton*)sender{
    for (UIButton * but in self.buttonsArray) {
        but.selected = NO;
        sender.selected = YES;
        if (but.selected) {
            but.backgroundColor = TM_ThemeBackGroundColor;
        }else{
            but.backgroundColor = TM_backgroundColor;
        }
    }
}
@end


